package com.company.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.exception.BusinessException;
import com.company.project.common.exception.code.BaseResponseCode;
import com.company.project.common.utils.CodeUtil;
import com.company.project.entity.SysStoreEntity;
import com.company.project.service.*;
import com.company.project.vo.resp.goods.TypeRespNodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.SysGoodsTypeMapper;
import com.company.project.entity.SysGoodsTypeEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("sysGoodsTypeService")
@Slf4j
public class SysGoodsTypeServiceImpl extends ServiceImpl<SysGoodsTypeMapper, SysGoodsTypeEntity> implements SysGoodsTypeService {
    @Autowired
    private SysStoreService sysStoreService;
    @Autowired(required = false)
    private SysGoodsTypeMapper sysGoodsTypeMapper;

    @Autowired
    private DeptService deptService;
    @Autowired
    private HttpSessionService httpSessionService;

    @Override
    public List<SysGoodsTypeEntity> selectAll(String storeId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(storeId)) {
            queryWrapper.eq("store_id", storeId);
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<SysGoodsTypeEntity> selectAll() {
        // 获取当前用户是否为root用户
        String deptId = deptService.rootDept();
        List<SysGoodsTypeEntity> sysGoodsTypeEntity;
        if (!deptId.equals("0")) {
            // 获取用户信息
            String userId = httpSessionService.getCurrentUserId();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userId);
            // 获取门店信息
            SysStoreEntity sysStoreEntity = sysStoreService.getOne(queryWrapper);
//            SysStoreEntity sysStoreEntity = sysStoreService.getMyStoreInfo();
            if (null == sysStoreEntity) {
                sysGoodsTypeEntity = this.selectAll(null);
            } else {
                sysGoodsTypeEntity = this.selectAll(sysStoreEntity.getId());
            }
        } else {
            sysGoodsTypeEntity = this.selectAll(null);
        }
        return sysGoodsTypeEntity;
    }

    @Override
    public List<TypeRespNodeVO> selectTree(String typeId) {

        SysGoodsTypeEntity sysGoodsTypeEntity = super.getById(typeId);

        return null;
    }

    private String getNewTypeCode() {
        Integer maxCode = sysGoodsTypeMapper.getMaxGoodsTypeCode();
        if (maxCode == null) {
            maxCode = 0;
        }
        Integer newCode = maxCode + 1;
        return CodeUtil.padRight(newCode, 6, "0");
    }

    @Override
    public List<TypeRespNodeVO> selectAllTree(String goodsTypeId) {
        List<SysGoodsTypeEntity> sysGoodsTypeEntities ;//= this.selectAll();

        if(StringUtils.isEmpty(goodsTypeId)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("store_id",httpSessionService.getCurrentStoreId());
            sysGoodsTypeEntities = super.list(queryWrapper);
        }else{
            SysGoodsTypeEntity sysGoodsTypeEntity = super.getById(goodsTypeId);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.likeRight("relation_code", sysGoodsTypeEntity.getRelationCode());
            List<String> childIds = super.listObjs(queryWrapper);
            queryWrapper = new QueryWrapper();
            queryWrapper.notIn("id",childIds);
            sysGoodsTypeEntities = super.list(queryWrapper);
        }
//        List<SysGoodsTypeEntity> sysGoodsTypeEntities = this.selectAll();
        List<TypeRespNodeVO> result = new ArrayList<>();
        TypeRespNodeVO root = new TypeRespNodeVO();
        root.setId("0");
        root.setTitle("默认顶级类型");
        root.setSpread(true);
        root.setChildren(getTree(sysGoodsTypeEntities));
        result.add(root);
        log.info(JSONObject.toJSONString(sysGoodsTypeEntities));
        log.info(JSONObject.toJSONString(result));
        return result;
    }

    @Override
    public boolean addGoodsType(SysGoodsTypeEntity sysGoodsType) {
        //通过用户查询门店
        String userId = httpSessionService.getCurrentUserId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        SysStoreEntity sysStoreEntity = sysStoreService.getOne(queryWrapper);
        if (null != sysGoodsType) {
            sysGoodsType.setStoreId(sysStoreEntity.getId());
        } else {
            sysGoodsType.setStoreId("0");
        }
        // goodsTypeNo
        String newCode = getNewTypeCode();
        sysGoodsType.setGoodsTypeNo(newCode);

        // 等级 and relation
        SysGoodsTypeEntity pGoodsType = super.getById(sysGoodsType.getPid());
        if (null != pGoodsType) {
            int level = Integer.valueOf(pGoodsType.getLevel()) + 1;
            sysGoodsType.setLevel(String.valueOf(level));
            sysGoodsType.setRelationCode(pGoodsType.getGoodsTypeNo() + newCode);
        } else {
            sysGoodsType.setLevel("0");
            sysGoodsType.setRelationCode(newCode);
        }
        sysGoodsType.setCreateTime(new Date());
        return super.save(sysGoodsType);
    }

    @Override
    public void updateType(SysGoodsTypeEntity sysGoodsType) {
        //
        SysGoodsTypeEntity sysGoodsTypeEntity = super.getById(sysGoodsType.getId());
        if(null == sysGoodsType){
            new BusinessException(BaseResponseCode.SYSTEM_BUSY);
        }
        if(sysGoodsType.getPid() != sysGoodsTypeEntity.getPid()){
            SysGoodsTypeEntity pType = super.getById(sysGoodsType.getPid());
            sysGoodsType.setRelationCode(pType.getRelationCode() + sysGoodsType.getGoodsTypeNo());
        }
        sysGoodsType.setUpdateTime(new Date());
        super.updateById(sysGoodsType);
    }

    private List<TypeRespNodeVO> getTree(List<SysGoodsTypeEntity> all) {
        List<TypeRespNodeVO> list = new ArrayList<>();
        if (all == null || all.isEmpty()) {
            return list;
        }
        for (SysGoodsTypeEntity sysGoodsTypeEntity : all) {
            TypeRespNodeVO treeRespVO = new TypeRespNodeVO();
            if (sysGoodsTypeEntity.getPid().equals("0")) {
                treeRespVO.setId(sysGoodsTypeEntity.getId());
                treeRespVO.setPid(sysGoodsTypeEntity.getPid());
                treeRespVO.setTitle(sysGoodsTypeEntity.getName());
                treeRespVO.setChildren(getChild(treeRespVO.getId(), all));

            }
            list.add(treeRespVO);
        }
        return list;
    }

    private List<TypeRespNodeVO> getChild(String id, List<SysGoodsTypeEntity> all) {
        List<TypeRespNodeVO> list = new ArrayList<>();
        for (SysGoodsTypeEntity sysGoodsTypeEntity : all) {
            if (sysGoodsTypeEntity.getPid().equals(id)) {
                TypeRespNodeVO typeRespNodeVO = new TypeRespNodeVO();
                typeRespNodeVO.setId(sysGoodsTypeEntity.getId());
                typeRespNodeVO.setTitle(sysGoodsTypeEntity.getName());
                typeRespNodeVO.setPid(sysGoodsTypeEntity.getPid());
                typeRespNodeVO.setDisabled(true);
                typeRespNodeVO.setChildren(this.getChild(typeRespNodeVO.getId(), all));
                list.add(typeRespNodeVO);
            }
        }
        return list;
    }


}